/**
 * @(#) ReqDExpenseMediator.as
 *
 * @date		2009. 02. 17.
 * @version		1.0 ?
 * @author		Shin, Yunwoo
 * @description	제증명신청 - Mediator
 * 				제증명 발행을 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.dexpense.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.approval.RequestApprovalMediator;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.dexpense.model.DExpenseApprovalProxy;
	import com.skt.common_hr.dexpense.model.DExpenseListProxy;
	import com.skt.common_hr.dexpense.model.DExpenseProxy;
	import com.skt.common_hr.dexpense.model.vo.ReqDExpenseVO;
	import com.skt.common_hr.dexpense.view.components.ReqDExpense;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;	

	public class ReqDExpenseMediator extends CommonMediator
	{
		public static const NAME:String = "ReqDExpenseMediator"; 
		
		public static const EXEC_NEW_REQUEST:String = "execNewRequest"; 
		public static const EXEC_MODI_REQUEST:String = "execModifyRequest"; 
		public static const EXEC_READ_REQUEST:String = "execReadRequest"; 
		public static const CLOSED_REQUEST_VIEW:String = "closedRequestView";

		public var key:String = "";
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ReqDExpenseMediator(viewComponent:Object)
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
			setEventListener();			
 		}
 		
 		private function initView():void
 		{
 			key = "";
			view.changeMode(ReqDExpense.MODE_MODIFY);
			view.selectedDate = new Date();
			view.txtComment.text = ""; 	
			view.appComment = "";		
			
			var listProxy:DExpenseListProxy = facade.retrieveProxy(DExpenseListProxy.NAME) as DExpenseListProxy;
			var start:Date = new Date(listProxy.BEGDA);
			var end:Date = new Date(listProxy.ENDDA);
			start.date--;
			end.date++;
			view.dfDinner.disabledRanges = [{rangeEnd:start}, {rangeStart:end}];
 		}
 		
 		private function setEventListener():void
 		{
			view.addEventListener(ReqDExpense.CLICK_OK, onClickOK);
			view.addEventListener(ReqDExpense.CLICK_CANCEL, onClickCancel);
 		}
 		
 		private function onClickOK(e:Event):void
 		{
 			save();
 		}
 		
 		private function onClickCancel(e:Event):void
 		{
 			if(key!="" && view.currentMode == ReqDExpense.MODE_MODIFY) {
 				delTempRequest(key);
 			}
 			else {
 				closeView();
 			}
 		}
 		
 		public function closeView():void
 		{
 			PopUpManager.removePopUp(view);
 			facade.sendNotification(CLOSED_REQUEST_VIEW, this);
 		}
 		
 		private function loadDetailData(key:String):void
 		{
 			var proxy:DExpenseProxy = facade.retrieveProxy(DExpenseProxy.NAME) as DExpenseProxy;
 			this.key = key;
 			proxy.load(key);
 		}
 		
 		private function delTempRequest(key:String):void
 		{
 			var proxy:DExpenseProxy = facade.retrieveProxy(DExpenseProxy.NAME) as DExpenseProxy;
 			proxy.del(key);
 		}
 
 		private var _savingData:Object;
 		
 		private function save():void
 		{
 			var proxy:DExpenseProxy = facade.retrieveProxy(DExpenseProxy.NAME) as DExpenseProxy;

 			var table:Array  = new Array();
 			_savingData = new Object();
 			table.push(_savingData);
 			
 			_savingData.PERNR = Variables.myPernr;
 			_savingData.BEGDA = DateUtil.toBasicString(view.selectedDate);
 			_savingData.TEXT1 = view.txtComment.text;
 			
 			proxy.save(key, table);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					EXEC_NEW_REQUEST,
					EXEC_READ_REQUEST,
					EXEC_MODI_REQUEST,
					DExpenseProxy.LOAD_DINNER_COMPLETE,
					DExpenseProxy.LOAD_DINNER_FAILED,
					DExpenseProxy.SAVE_DINNER_COMPLETE,
					DExpenseProxy.SAVE_DINNER_FAILED,
					DExpenseProxy.DEL_DINNER_COMPLETE,
					RequestApprovalMediator.APPROVAL_CANCEL,
					RequestApprovalMediator.APPROVAL_COMPLETE
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case EXEC_NEW_REQUEST:
					initView();
					break;
				case EXEC_MODI_REQUEST:
					initView();
					loadDetailData(note.getBody() as String);
					break;
				case EXEC_READ_REQUEST:
					view.changeMode(ReqDExpense.MODE_READONLY1);
					loadDetailData(note.getBody() as String);
					break;
				case DExpenseProxy.LOAD_DINNER_COMPLETE:
					onLoadDinner(DExpenseProxy(note.getBody()));
					break;
				case DExpenseProxy.SAVE_DINNER_COMPLETE:
					onSavedDinner(DExpenseProxy(note.getBody()));
					break;
				case DExpenseProxy.DEL_DINNER_COMPLETE:
					closeView();
					break;
				case DExpenseProxy.LOAD_DINNER_FAILED:
				case DExpenseProxy.SAVE_DINNER_FAILED:
					CommonAlert.showMessage("", "("+ DExpenseProxy(note.getBody()).retCode + ") " +DExpenseProxy(note.getBody()).retText);
					break;
				case RequestApprovalMediator.APPROVAL_COMPLETE:
					closeView();
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		/**
		 * Main Module에서 QueryClose 메세지가 왔을 때의 처리
		 *     - 사용자에게 확인하고 View에 종료처리 -> MainModule에 'CLOSE_MODULE' 전송
		 * @param
		 * @return void
		 */
		private var loadedDP:ReqDExpenseVO = null;
		private function onLoadDinner(proxy:DExpenseProxy):void
		{
			loadedDP = proxy.dp;
			view.selectedDate = loadedDP.BEGDA;
			view.txtComment.text = loadedDP.TEXT1;
			view.appComment = loadedDP.TEXT2;
			
			if(view.currentMode!=ReqDExpense.MODE_MODIFY) {
				if(StringUtil.NVL(view.appComment) == "") {
					view.changeMode(ReqDExpense.MODE_READONLY2);
				}
			}
		}
		
		private function onSavedDinner(proxy:DExpenseProxy):void
		{
			key = proxy.key;
			execApproval(proxy.key);
		}
		
 		private function execApproval(key:String):void
 		{
 			
 			var approval:RequestApprovalMediator = facade.retrieveMediator(RequestApprovalMediator.NAME) as RequestApprovalMediator;
 			if(approval==null) {
 				approval = new RequestApprovalMediator(view, "10");
 				facade.registerMediator(approval);
 			}
 			
 			var appProxy:DExpenseApprovalProxy = facade.retrieveProxy(DExpenseApprovalProxy.NAME) as DExpenseApprovalProxy;
 			_savingData.GWAREKEY = appProxy.key = key;
 			_savingData.DAY = view.selectedDate.day;
 			appProxy.dataProvider = _savingData;
 			approval.execRequest(view.parentApplication as DisplayObject, appProxy);
 		}

 		private function cancelApproval(key:String):void
 		{
 			//var proxy:ReqVacationProxy = facade.retrieveProxy(ReqVacationProxy.NAME) as ReqVacationProxy;
 			//proxy.del([{GWAREKEY: key}]);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():ReqDExpense
        {
            return viewComponent as ReqDExpense;
        }
		
		
	}
}